﻿using FNN.Factories.SourcePoints;
using FNN.Helpers;
using FNN.Helpers.DataSource;
using FNN.Helpers.IO;
using FNN.Networks;
using FNN.SourcePoints;
using Microsoft.Win32;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace GraphicFNN.Pages
{
    /// <summary>
    /// Логика взаимодействия для ActivityPage.xaml
    /// </summary>
    public partial class ActivityPage : Page
    {
        public SerializationHelper serializationHelper = new SerializationHelper();

        public ActivityPage()
        {
            InitializeComponent();
        }

        private void BtnSelectSource_Click(object sender, RoutedEventArgs e)
        {
            var fileBrowser = new OpenFileDialog
            {
                CheckFileExists = true,
                Multiselect = false,
                Filter = "FNN|*.fnn|XML|*.xml|SOAP|*.soap"
            };
            fileBrowser.ShowDialog();
            TxtNetworkPath.Text = fileBrowser.FileName;
        }

        private void BtnShowActivity_Click(object sender, RoutedEventArgs e)
        {
            var targetNetwork = (FinancialNetwork)serializationHelper.LoadBinary(TxtNetworkPath.Text);
            if (targetNetwork == null)
            {
                MessageBox.Show("Ошибка! Файл поврежден или имел неверный формат!");
                return;
            }
                       
            var networkPath = Path.GetDirectoryName(TxtNetworkPath.Text) + "\\" + targetNetwork.NetworkName + ".csv";
            if (File.Exists(networkPath))
            {
                NetworkActivityWindow dialog = new NetworkActivityWindow();
                var helper = new DataSourceHelper(
                    new CsvHelper(networkPath),
                    new DateSourcePointFactory()
                );
                dialog.ShowNetworkActivity(targetNetwork, helper.GetDataSource().Select(o => (DateSourcePoint) o));
                dialog.Show();
            }
            else
            {
                MessageBox.Show("Файл с исходными данными не найден! Возможно, он был перемещен или удален.");
            }
        }
    }
}
